package com.dawnling.androidutils.view;
 
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.Rect;
import android.graphics.RectF;
import android.util.AttributeSet;
import android.util.TypedValue;
import android.view.View;

import com.dawnling.androidutils.R;
import java.text.DecimalFormat;
 
/**
 * 自定义百分比左右进度条，根据两个值的百分比不同，在一个进度条中显示出来，中间的隔线为斜线
 * Created by cg on 2016/7/28 0028.
 */
public class CustomProgress extends View {
    private float iNum = 50;                     //进(左)的数量
    private int iColor = Color.RED;              //进的颜色
    private float oNum = 50;                     //出(右)的数量
    private int oColor = Color.GREEN;            //出的颜色
    private int mInclination = 40;               //两柱中间的倾斜度
    private int iTextColor = Color.WHITE;        //进的百分比数字颜色
    private int oTextColor = Color.WHITE;        //出的百分比数字颜色
    private int TextSize = 30;                   //百分比字体大小
    private int width ;
    private int height ;
 
    private float iPre;
    private float oPre;
 
    private String txtiPre;                      //显示进的百分比
    private String txtoPre;                      //显示出的百分比
 
    private Paint mPaint;
    private Rect mBound;                        //包含文字的框
 
    public CustomProgress(Context context){ this(context, null); }
 
    public CustomProgress(Context context, AttributeSet attrs){ this(context, attrs, 0); }
 
    public CustomProgress(Context context, AttributeSet attrs, int defStyleAttr){
        super(context, attrs, defStyleAttr);
 
        mPaint = new Paint();
        mPaint.setAntiAlias(true);
        mPaint.setStyle(Paint.Style.FILL);
        mPaint.setStrokeWidth(5);
 
        mBound = new Rect();
    }
 
    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        int widthMode = MeasureSpec.getMode(widthMeasureSpec);
        int widthSize = MeasureSpec.getSize(widthMeasureSpec);
 
        int heightMode = MeasureSpec.getMode(heightMeasureSpec);
        int heightSize = MeasureSpec.getSize(heightMeasureSpec);

        if(widthMode==MeasureSpec.EXACTLY) width = widthSize;
        else width = getPaddingLeft() + getWidth() + getPaddingRight();
        if(heightMode==MeasureSpec.EXACTLY) height = heightSize;
        else height = getPaddingTop() + getHeight() + getPaddingBottom();
        setMeasuredDimension(width,height);
    }
 
 
    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);

        iPre = (iNum /(iNum + oNum)) * getWidth();
        oPre = (oNum /(iNum + oNum)) * getWidth();
        //如果进值或出值有一个为0，则另一个就会占满整个进度条，这时就不需要倾斜角度了
        if(iNum == 0 || oPre == 0) mInclination = 0;

        //左边画笔
        Path iPath = new Path();
        iPath.moveTo(0, 0);
        iPath.lineTo(iPre + mInclination, 0);
        iPath.lineTo(iPre, getHeight());
        iPath.lineTo(0, getHeight());
        iPath.close();

//        int[] SECTION_COLORS = new int[]{R.color.colorOrange, R.color.colorBlue};
//        LinearGradient shader = new LinearGradient(0, height, iPre, height, SECTION_COLORS, null, Shader.TileMode.MIRROR);
//        mPaint.setShader(shader);
        mPaint.setColor(iColor);
        canvas.drawPath(iPath, mPaint);

        //右边画笔
        Path oPath = new Path();
        oPath.moveTo(iPre + mInclination, 0);
        oPath.lineTo(getWidth(), 0);
        oPath.lineTo(getWidth(), getHeight());
        oPath.lineTo(iPre - mInclination, getHeight());
        oPath.close();
 
        mPaint.setColor(oColor);
        canvas.drawPath(oPath, mPaint);
 
        txtiPre = getProValText(iNum /(iNum + oNum) * 100);
        txtoPre = getProValText(oNum /(iNum + oNum) * 100);

        mPaint.setColor(iTextColor);
        mPaint.setTextSize(TextSize);
 
        mPaint.getTextBounds(txtiPre, 0, txtiPre.length(), mBound);
        //判断一下，如果进值为0则不显示，如果进值不为空而出值为0，则进值的数值显示居中显示
        if(iNum != 0 && oNum != 0) {
            canvas.drawText(txtiPre, 20, getHeight() / 2 + mBound.height() / 2, mPaint);
        }else if(iNum!=0 && oNum==0){
            canvas.drawText(txtiPre, getWidth()/2 - mBound.width()/2, getHeight() / 2 + mBound.height() / 2, mPaint);
        }
 
        mPaint.setColor(oTextColor);
        mPaint.getTextBounds(txtoPre, 0, txtoPre.length(), mBound);
        if(oNum != 0 && iNum != 0) {
            canvas.drawText(txtoPre, getWidth() - 20 - mBound.width(), getHeight() / 2 + mBound.height() / 2, mPaint);
        }else if(oNum != 0 && iNum == 0){
            canvas.drawText(txtoPre, getWidth()/2 - mBound.width()/2, getHeight() / 2 + mBound.height() / 2, mPaint);
        }
    }
 
    /**
     * 格式化显示的百分比
     * @param proValue
     * @return
     */
    private String getProValText(float proValue){
        DecimalFormat format = new DecimalFormat("#0.0");
        return format.format(proValue) + "%";
    }
 
    /**
     * 动态设置进值
     * @param iNum
     */
    public void setINum(float iNum){
        this.iNum = iNum;
        postInvalidate();
    }
 
    /**
     * 动态设置出值
     * @param oNum
     */
    public void setONum(float oNum){
        this.oNum = oNum;
        postInvalidate();
    }
}
